package com.yl.chainStore.control.user

import com.yl.chainStore.control.BaseCtrl
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import com.yl.users.common.util.permissionsViewListToTree
import com.yl.users.common.util.permissionsViewListToTreeSelect
import com.yl.users.entity.CompanyPermissionsView
import com.yl.users.services.ICompanyPermViewService
import com.yl.users.services.ICompanyRoleService
import com.yl.users.services.ISysCompanyService
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.interceptor.TransactionAspectSupport
import org.springframework.web.bind.annotation.*
import java.lang.Exception

/**
 * 公司菜单管理视图层
 */
@RestController
@RequestMapping("company/user/perm")
open class CompanyPermissionsViewController(
    private val iSysCompanyService: ISysCompanyService,
    private val iCompanyRoleService: ICompanyRoleService,
    private val iCompanyPermViewService: ICompanyPermViewService
) : BaseCtrl() {

    /**
     * 获取目录树形图
     */
    @GetMapping("getDirectoryTree")
    @Transactional
    open fun getDirectoryTree(): ResponseBody {
        val companyId = getContext().companyId
        val list = iSysCompanyService.getCompanyPermViewDirectoryList(companyId)
        return returnSuccess(permissionsViewListToTreeSelect(list, 0L))
    }



    @GetMapping("detail/{id}")
    @Transactional
    open fun detail(@PathVariable id: Long): ResponseBody {
        val companyPerm = iCompanyPermViewService.getCompanyPermViewDetail( id)
            ?: return returnError(ErrorCode.DATA_IS_NOT_FIND)
        return returnSuccess(companyPerm)
    }

    @PostMapping("add")
    @Transactional
    open fun add(@RequestBody view: CompanyPermissionsView): ResponseBody {
        view.companyId = getContext().companyId
        view.createType = 2
        view.permissionsType = 1
        return iSysCompanyService.addCompanyPermViewByCompany(view)
    }

    @PostMapping("update")
    @Transactional
    open fun update(@RequestBody view: CompanyPermissionsView): ResponseBody {
        view.companyId = getContext().companyId
        view.createType = 2
        return iCompanyPermViewService.updateCompanyPermView(view)
    }

    @PostMapping("del/{id}")
    @Transactional(rollbackFor = [Exception::class])
    open fun del(@PathVariable id: Long): ResponseBody {
        val delCompanyPermViewByCompany = iCompanyPermViewService.delCompanyPermView(id)
        if(delCompanyPermViewByCompany.code != ErrorCode.SUCCESS.code){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
            return delCompanyPermViewByCompany
        }
        return returnSuccess()
    }

    /**
     * 获取树形图
     */
    @GetMapping("getTree")
    @Transactional
    open fun getTree(): ResponseBody {
        val companyId = getContext().companyId
        val list = iSysCompanyService.getCompanyPermViewList(companyId)
        return returnSuccess(permissionsViewListToTreeSelect(list,0L))
    }


}
